< previous page page_184 next page >

Page 184
Call by Value and Call by Reference
You can use two different methods for passing in parameters to a procedure: call by value and call by reference. Theres a big difference between these two methods, as youll see in this section.
Examples Filename: PassParms.frm and Func.bas
Call by Reference
By default, Visual Basic passes all arguments to procedures by reference. This means that the routine accepting the parameters can actually change them. As an example, lets take another look at the example that takes the values from the text boxes and assigns them to variables:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdSave_Click()
   Dim strFirst As String
   Dim strLast As String

   strFirst = txtFirst.Text
   strLast = txtLast.Text

   EmployeeDisplay strFirst, strLast
   Call EmployeeDisplay(strFirst, strLast)
End Sub
The strFirst and strLast variables are local to the cmdSave_Click() event procedure. This means that no other routine can see or affect these variables, right? Well, not exactly. The EmployeeDisplay() procedure can change these variables if the arguments are created by reference. In the EmployeeDisplay procedure, the two arguments are declared As String. This means that theyre being passed in by reference. Its the same as placing the keyword ByRef in front of the arguments, as shown here:
9f31265bb40b592b1929e18a47ab28e5.gif
Public Sub EmployeeDisplay(ByRef strFirst As String, _
                         ByRef strLast As String)
   Dim strEmp As String

   strEmp = "My Last Name is " & strLast
   strEmp = strEmp & " and my first name is " & strFirst

   EmployeeDisplay = strEmp
End Sub

 
< previous page page_184 next page >

If you like this book, buy it!